<?php
Prado::using('Application.common.common');
class ManagePhotos extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		if (!$this->IsPostBack)
		{
			$this->Master->pageHead->Title = "Admin | Manage Photos";
			$this->AdminMenu->lnkManageFoodPlace->CssClass = "active";
			
			$this->txtFoodPlaceID->Value = ($this->Request->contains('fid')) ? TPropertyValue::ensureInteger($this->Request['fid']) : 0;		
			$foodPlaceRecord = FoodPlaceRecord::finder()->findByPk($this->txtFoodPlaceID->Value);
			$this->lblFoodPlaceName->Text = $foodPlaceRecord->Name;				
			$this->populatePhotos();	
			
			if ($this->Request->contains('m')) {
				$this->Notice->Type = UserNoticeType::NoticeAdmin;
				if ($this->Request['m'] == 'cp')
					$this->Notice->Text = $this->Application->getModule('message')->translate('ADD_SUCCESS','Photos','');
				else if ($this->Request['m'] == 'dp')
					$this->Notice->Text = $this->Application->getModule('message')->translate('DELETE_SUCCESS','Photo','');	
			}
		}
	}
	
	protected function populatePhotos()
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = "food_place_id = " . $this->txtFoodPlaceID->Value;				
		$criteria->OrdersBy["sort_order"] = "asc";							
		$activeRecord = Prado::createComponent("PhotoRecord");
		$limit = null;
		$offset = null;
		$criteria->Limit = $limit;
		$criteria->Offset = $offset;
		$items = $activeRecord->finder()->findAll($criteria);
		$this->ItemList_Request->DataSource = $items;
		$this->ItemList_Request->dataBind();
		$this->lblTotal->Text="- Photos (" . count($items) . ")";		
		if (count($items) <= 0)
		{
			$this->lblTotal->Text="- There is no photo for this food place.";
			$this->lblTotal->Style="text-transform:lower;";
		}
	}
	
	protected function list_ItemCreated($sender, $param)
	{
		if ($param->Item->ItemType == "Item" || $param->Item->ItemType == "AlternatingItem")
		{
			if ($param->Item->Data)
			{				
				$file = $param->Item->Data->PhotoFile;
				$info = pathinfo($file);
				$file_name =  basename($file,'.'.$info['extension']); 
				$currentYear = date('Y', $param->Item->Data->CreateDate);
				$currentMonth = date('m', $param->Item->Data->CreateDate);
				$param->Item->colImg->imgPhoto->ImageUrl = $this->Request->UrlManagerModule->UrlPrefix."/useruploads/photos/food-place/".$currentYear."/".$currentMonth."/".$file_name.$this->Application->Parameters['PHOTO_LIST_AFFFIX'].".".$info['extension'];					
			}
		}
	}	
	
	protected function list_ItemCommand($sender, $param)
	{
		switch($param->CommandName)
		{
			case "delete":		
				$criteria = new TActiveRecordCriteria;
				$criteria->Condition = "photo_id = " .$param->Item->colButton->txtPhotoID->Value;
				PhotoRecord::finder()->deleteAll($criteria);	
				$this->Response->redirect($this->Service->ConstructUrl('admin.ManagePhotos', array('fid'=>$this->txtFoodPlaceID->Value, 'm'=>'dp')));			
				break;
			default:
				break;
		}

		$this->populatePhotos();	
	}
	
	protected function btnSubmit_Clicked($sender, $param)
	{
		if ($this->IsValid)
		{						
			try {
				$user = UserRecord::finder()->findByPk($this->Application->User->ID);
				if ($user instanceof UserRecord)
				{						
					$currentYear = date("Y");
					$currentMonth = date("m");
					$filePath = dirname($this->Request->ApplicationFilePath).DIRECTORY_SEPARATOR."useruploads".DIRECTORY_SEPARATOR."photos".DIRECTORY_SEPARATOR."food-place".DIRECTORY_SEPARATOR.$currentYear.DIRECTORY_SEPARATOR;
					Common::check_dir($filePath);
					$filePath = $filePath.$currentMonth.DIRECTORY_SEPARATOR;				
					Common::check_dir($filePath);
					
					if($this->fuPhoto1->HasFile) 
					{
						Common::savePhoto($this->fuPhoto1, $filePath, 1, $this->txtFoodPlaceID->Value, $user->ID);						
					}	
					
					if($this->fuPhoto2->HasFile) 
					{
						Common::savePhoto($this->fuPhoto2, $filePath, 2, $this->txtFoodPlaceID->Value, $user->ID);						
					}
					
					if($this->fuPhoto3->HasFile) 
					{
						Common::savePhoto($this->fuPhoto3, $filePath, 3, $this->txtFoodPlaceID->Value, $user->ID);						
					}
					
					if($this->fuPhoto4->HasFile) 
					{
						Common::savePhoto($this->fuPhoto4, $filePath, 4, $this->txtFoodPlaceID->Value, $user->ID);						
					}
					
					if($this->fuPhoto5->HasFile) 
					{
						Common::savePhoto($this->fuPhoto5, $filePath, 5, $this->txtFoodPlaceID->Value, $user->ID);						
					}
					
					if($this->fuPhoto6->HasFile) 
					{
						Common::savePhoto($this->fuPhoto6, $filePath, 6, $this->txtFoodPlaceID->Value, $user->ID);						
					}
								
					$this->Response->redirect($this->Service->ConstructUrl('admin.ManagePhotos', array('fid'=>$this->txtFoodPlaceID->Value, 'm'=>'cp')));			
					
				}
				else
				{					
					$this->Notice->Type = UserNoticeType::Error;
					$this->Notice->Text = $this->Application->getModule('message')->translate('ITEM_NOT_FOUND','user');
				}
			}
			catch(TException $e)
			{
				$this->Notice->Type = UserNoticeType::Error;
				$this->Notice->Text = $e;
			}
		}
	}
	
	protected function btnCancel_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("admin.ManageFoodPlaces"));				
	}
}

?>